package com.example.demo.controller;

import com.example.demo.test.executor.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 *
 * 测试  异步操作
 * Created by Administrator on 2018/4/15.
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private Task task;


    @RequestMapping("task")
    public String testTask() throws Exception {
        long start = System.currentTimeMillis();

        Thread.sleep(1000);
        Future<Long> stringFuture1 = task.task1();
        Future<Long> stringFuture2 = task.task2();
        Future<Long> stringFuture3 = task.task3();
        long end = System.currentTimeMillis();
        while (!stringFuture1.isDone() || !stringFuture2.isDone() || !stringFuture3.isDone()){
            if(stringFuture1.isDone() && stringFuture2.isDone() && stringFuture3.isDone()){
                break;
            }
        }
        System.out.println(end-start);
        return (end-start)+"";
    }


}
